package cn.geminis.data.jpa.graphql.entity;

import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@MappedSuperclass
@Getter
@Setter
public abstract class LevelOneBase {

    @Id
    @Column(length = 36)
    private String id;

    @Column(nullable = false, length = 256)
    private String name;

    @Column(nullable = false)
    private int amount;

    @OneToMany(mappedBy = "levelOne", cascade = CascadeType.ALL)
    private Set<LevelTwo> levelTwos = new HashSet<>();

    @ManyToMany(mappedBy = "levelOnes", cascade = CascadeType.MERGE)
    private Set<LevelOneOther> levelOneOthers = new HashSet<>();

    @PrePersist
    void createId() {
        if (StringUtils.isEmpty(this.id)) {
            this.id = UUID.randomUUID().toString();
        }
    }
}
